////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
//  This software for customer relationship management system, developed by Iflytek-ZY-GCZX team.
//  Software code and design for the team, copy rights reserved.
////////////////////////////////////////////////////////////////////////////////

package com.iflytek.ci.qams.service.impl;

import java.util.Date;
import java.util.Map;

import com.iflytek.ci.qams.domain.Issue;
import com.iflytek.ci.qams.dto.IssueFulltext;
import com.iflytek.framework.lucene.Indexs;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.Term;

/**
 * <p>
 * 全文索引任务.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 2012-11-26 10:59 AM
 * @since JDK 1.5
 */
public class FullTextIndexTask implements Runnable {

	/** 问题信息 */
	private final Issue issue;
	/** 其他信息 */
	private final Map<String, Object> issue_other_info;
	/** 索引服务 */
	private final Indexs indexs;

	/**
	 * 构建一个索引任务
	 *
	 * @param issue            问题信息
	 * @param issue_other_info 问题其他信息
	 * @param indexs           索引建立
	 */
	public FullTextIndexTask(Issue issue, Map<String, Object> issue_other_info, Indexs indexs) {
		this.issue = issue;
		this.issue_other_info = issue_other_info;
		this.indexs = indexs;
	}

	@Override
	public void run() {
		//将 issue 转换为 Document
		final Document document = new Document();
		document.add(new StringField(IssueFulltext.PKEY, issue.getPkey(), Field.Store.YES));
		document.add(new IntField("priopity", issue.getPriopity(), Field.Store.YES));
		document.add(new IntField("issuetype", issue.getIssuetype(), Field.Store.YES));
		document.add(new IntField("issuelabel", issue.getIssuelabel(), Field.Store.NO));
		document.add(new LongField("pid", issue.getPid(), Field.Store.YES));
		document.add(new TextField(IssueFulltext.SUMMARY, issue.getSummary(), Field.Store.YES));
		document.add(new TextField(IssueFulltext.DESCRIPTION, issue.getDescription(), Field.Store.NO));
		document.add(new TextField("environment", issue.getEnvironment() == null ? "" : issue.getEnvironment(), Field.Store.NO));
		Date created = issue.getCreated();
		//排序需要存储
		document.add(new LongField("created", (created == null ? 0 : created.getTime()), Field.Store.YES));
		Date updated = issue.getUpdated();
		document.add(new LongField("updated", (updated == null ? 0 : updated.getTime()), Field.Store.YES));
		document.add(new StringField("reporter", issue.getReporter(), Field.Store.YES));
		document.add(new StringField("reporterMail", issue.getReporterMail() == null ? "" : issue.getReporterMail(), Field.Store.NO));
		document.add(new StringField("assignee", issue.getAssignee() == null ? "" : issue.getAssignee(), Field.Store.NO));
		document.add(new StringField("assigneeName", issue.getAssigneeName() == null ? "" : issue.getAssigneeName(), Field.Store.YES));
		//删除索引的字段标记
		final Term key_term = new Term(IssueFulltext.PKEY, issue.getPkey());
		indexs.create_index(document, key_term);
	}
}
